﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elderos.Highlight.Search;
using Elderos.Ontology;

namespace Elderos.Highlight.Rules
{
    [RankingRule("ArticleSize", RuleType.Normalizing)]
    public class RuleArticleSize : RankingRule
    {
        public RuleArticleSize(IOntologyData ontology) : base(ontology)
        {
        }

        private readonly string[] _factorNames =
        {
            "ArticleSize_EntityCount",
            "ArticleSize_TextLength"
        };

#pragma warning disable 1998
        protected override async Task<Dictionary<TextEntity, IRuleResult>> InnerRank(HighlightResult highlightResult)
#pragma warning restore 1998
        {
            var result = new Dictionary<TextEntity, IRuleResult>();
            int entityCount = highlightResult.Positions.SelectMany(x => x.EntitiesFlattened).Count();
            int textLength = highlightResult.Text.Length;
            foreach (var entity in highlightResult.Positions.SelectMany(x => x.EntitiesFlattened))
            {
                result.Add(entity, new ArrayResult(_factorNames, new double[] { entityCount, textLength }));
            }
            return result;
        }

        public override IRuleResult EmptyResult
        {
            get { return new ArrayResult(_factorNames, new double[] { 0, 0 }); }
        }
    }
}
